package com.data.service.manage.impl;

import com.data.domain.manage.Consigninfo;
import com.data.domain.manage.ConsigninfoCriteria;
import com.data.domain.manage.Consigninfo;
import com.data.domain.manage.ConsigninfoCriteria;
import com.data.mapper.manage.ConsigninfoMapper;
import com.data.model.ManagerModel;
import com.data.service.manage.ConsigninfoService;
import com.data.utill.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service(value = "consigninfoService")
public class ConsigninfoServiceImpl implements ConsigninfoService {

    @Autowired
    private ConsigninfoMapper consigninfoMapper;

    @Override
    public List<Consigninfo> getConsigninfo(ManagerModel managerModel){
        ConsigninfoCriteria consigninfoCriteria=new ConsigninfoCriteria();
        ConsigninfoCriteria.Criteria criteria=consigninfoCriteria.createCriteria();
        //协议所属机构
        if(StringUtils.isNoNullString(managerModel.getOrgcode())){
            criteria.andOrgcodeEqualTo(StringUtils.getIdFromString(managerModel.getOrgcode()));
        }
        //协议状态
        if(StringUtils.isNoNullString(managerModel.getState())){
            criteria.andStateEqualTo(Short.parseShort(managerModel.getState()));
        }
        //根据地区控制显示
        if (managerModel.getOrgcodelist().size()>0 && !managerModel.getAreacode().equals("3300")){
            criteria.andOrgcodeIn(managerModel.getOrgcodelist());
        }
        //协议计量号码
        if(StringUtils.isNoNullString(managerModel.getMeternumb())){
            criteria.andMeternumbEqualTo(managerModel.getMeternumb());
        }
        //付款账户账号
        if(StringUtils.isNoNullString(managerModel.getDbtracct())){
            criteria.andDbtracctLike("%"+managerModel.getDbtracct()+"%");
        }
        //付款账户开户银行
        if(StringUtils.isNoNullString(managerModel.getDbtrbank())){
            criteria.andDbtrbankLike("%"+managerModel.getDbtrbank()+"%");
        }
        consigninfoCriteria.setOrderByClause("workdate asc");
        return consigninfoMapper.selectByExample(consigninfoCriteria);
    }
    /**
     * 入网机构信息插入
     * @param managerModel
     * @return
     */
    @Override
    public int insertConsigninfo(ManagerModel managerModel) {
        ConsigninfoCriteria consigninfoCriteria=new ConsigninfoCriteria();
        Consigninfo data = new Consigninfo();
//        data.setId(Long.parseLong(managerModel.getId()));
        data.setOrgcode(managerModel.getOrgcode());
//        data.setConsignnumb(managerModel.getConsignnumb());
//        data.setAcctbank(managerModel.getAcctbank());
//        data.setAccount(managerModel.getAccount());
//        data.setAcctname(managerModel.getAcctname());
//        data.setAccttype(Short.parseShort(managerModel.getAccttype()));
        data.setAgtid(managerModel.getAgtid());
        data.setDbtrbank(managerModel.getDbtrbank());
        data.setDbtracct(managerModel.getDbtracct());
        data.setDbtrname(managerModel.getDbtrname());
        data.setDbtrtype(Short.parseShort(managerModel.getDbtrtype()));
        data.setSignplace(managerModel.getSignplace());
        data.setMeternumb(managerModel.getMeternumb());
        data.setMetername(managerModel.getMetername());
        data.setPrtry(managerModel.getPrtry());
        data.setCerttype(managerModel.getCerttype());
        data.setCertcode(managerModel.getCertcode());
        data.setTel(managerModel.getTel());
        data.setEmail(managerModel.getEmail());
        data.setAddr(managerModel.getAddr());
        data.setState(Short.parseShort(managerModel.getState()));
        data.setCreatedate(managerModel.getCreatedate());
        data.setWorkdate(managerModel.getWorkdate());
        data.setLastupdate(new Date());
        return consigninfoMapper.insertSelective(data);
    }

    /**
     * 入网机构信息修改
     * @param managerModel
     * @return
     */
    @Override
    public int modifyConsigninfo(ManagerModel managerModel ,String  id) {
        ConsigninfoCriteria consigninfoCriteria=new ConsigninfoCriteria();
        consigninfoCriteria.createCriteria().andIdEqualTo(Long.parseLong(id));
        Consigninfo data = new Consigninfo();
//        data.setId(Long.parseLong(managerModel.getId()));
        data.setOrgcode(managerModel.getOrgcode());
        data.setAgtid(managerModel.getAgtid());
        data.setDbtrbank(managerModel.getDbtrbank());
        data.setDbtracct(managerModel.getDbtracct());
        data.setDbtrname(managerModel.getDbtrname());
        data.setDbtrtype(Short.parseShort(managerModel.getDbtrtype()));
        data.setSignplace(managerModel.getSignplace());
        data.setMeternumb(managerModel.getMeternumb());
        data.setMetername(managerModel.getMetername());
        data.setPrtry(managerModel.getPrtry());
        data.setCerttype(managerModel.getCerttype());
        data.setCertcode(managerModel.getCertcode());
        data.setTel(managerModel.getTel());
        data.setEmail(managerModel.getEmail());
        data.setAddr(managerModel.getAddr());
        data.setState(Short.parseShort(managerModel.getState()));
        data.setCreatedate(managerModel.getCreatedate());
        data.setWorkdate(managerModel.getWorkdate());
        data.setLastupdate(new Date());
        return consigninfoMapper.updateByExampleSelective(data,consigninfoCriteria);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Consigninfo getConsigninfoById(String  id) {
        ConsigninfoCriteria consigninfoCriteria=new ConsigninfoCriteria();
        consigninfoCriteria.createCriteria().andIdEqualTo(Long.parseLong(id));
        List<Consigninfo> data = consigninfoMapper.selectByExample(consigninfoCriteria);
        if (null!=data) {
            return data.get(0);
        }else{
            return null;
        }
    }
}
